# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand
from application import settings


class Command(BaseCommand):
    """
    项目初始化命令: python manage.py init
    """

    def add_arguments(self, parser):
        parser.add_argument(
            "init_name",
            nargs="*",
            type=str,
        )
        parser.add_argument("-y", nargs="*")
        parser.add_argument("-Y", nargs="*")
        parser.add_argument("-n", nargs="*")
        parser.add_argument("-N", nargs="*")
        parser.add_argument("-a", nargs="*")
        parser.add_argument("-A", nargs="*")

    def handle(self, *args, **options):
        reset = False
        if isinstance(options.get("y"), list) or isinstance(options.get("Y"), list):
            reset = True
        if isinstance(options.get("n"), list) or isinstance(options.get("N"), list):
            reset = False
        print(
            f"正在准备初始化数据，{'如有初始化数据，将会不做操作跳过' if not reset else '初始数据将会先删除后新增'}..."
        )

        assign_apps = options.get("a") or options.get("A") or []
        print(f"待更新的APP列表为：{assign_apps}")
        for app in settings.INSTALLED_APPS:
            if assign_apps and app not in assign_apps:
                continue
            try:
                exec(
                    f"""
from {app}.initialize import main
print(f"正在初始化模块：'{app}'...")
main(reset={reset})
print(f"初始化模块：'{app}'完毕！")
                """
                )
            except ModuleNotFoundError:
                pass
        print("==>初始化数据完成！")
